package cn.itwangxiang.android.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Locale;

import cn.itwangxiang.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class DemoPhoto extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_photo);

		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(
						DemoPhoto.this);
				builder.setTitle("获取图片的方式");
				builder.setItems(new String[] { "拍照", "从手机相册选择" },
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// 点击后弹出窗口选择了第几项
								switch (which) {
								case 0:
									Intent intent = new Intent(
											android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
									startActivityForResult(intent, 1);
									break;
								case 1:
									Intent openAlbumIntent = new Intent(
											Intent.ACTION_PICK,
											android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
									openAlbumIntent.setType("image/*");
									startActivityForResult(openAlbumIntent, 2);
									break;
								}
							}
						});
				builder.create().show();
			}
		});
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {

			String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.i("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}

			if (requestCode == 1) {

				String name = new DateFormat().format("yyyyMMdd_hhmmss",
						Calendar.getInstance(Locale.CHINA)) + ".jpg";
				Bundle bundle = data.getExtras();
				Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

				/*
				 * FileOutputStream fileOutputStream = null; File file = new
				 * File("/sdcard/test/"); file.mkdirs();// 创建文件夹 String fileName
				 * = "/sdcard/test/" + name;
				 * 
				 * try { fileOutputStream = new FileOutputStream(fileName);
				 * bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
				 * fileOutputStream);// 把数据写入文件 } catch (FileNotFoundException
				 * e) { e.printStackTrace(); } finally { try {
				 * fileOutputStream.flush(); fileOutputStream.close(); } catch
				 * (IOException e) { e.printStackTrace(); } }
				 */

				ImageView imageView = (ImageView) findViewById(R.id.imageView1);
				imageView.setImageBitmap(bitmap);

			} else if (requestCode == 2) {
				String name = new DateFormat().format("yyyyMMdd_hhmmss",
						Calendar.getInstance(Locale.CHINA)) + ".jpg";

				FileOutputStream fileOutputStream = null;
				File file = new File("/sdcard/test/");
				file.mkdirs();// 创建文件夹
				String fileName = "/sdcard/test/" + name;

				Uri imageFileUri = data.getData();

				BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

				bmpFactoryOptions.inJustDecodeBounds = false;
				Bitmap bitmap = null;
				try {
					bitmap = BitmapFactory
							.decodeStream(
									getContentResolver().openInputStream(
											imageFileUri), null, bmpFactoryOptions);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				ImageView imageView = (ImageView) findViewById(R.id.imageView1);
				imageView.setImageBitmap(bitmap);

			}

		}
	}
}
